<script>
    var houseId;
    function nextPage(pageNo){
        $("#house-listing").load("<?= $this->baseURI(); ?>planning/house/list/0/"+pageNo);
    }
    function nextPageSub(pageNo){
        houseId = $("#house_id").val();
        $("#remarks-listing").load("<?= $this->baseURI(); ?>planning/house/remarks/"+houseId+"/"+pageNo);
    }
    function editHouse(id){
        houseId = id;
        $("#edit-house-form").load("<?= $this->baseURI(); ?>planning/house/edit/"+id);        
        $("#edit-house-form" ).dialog( "open" );
    }
    function houseDetail(id,name){
        houseId = id;
        $("#house-detail").attr('title', name+' Detail');
        $(function() {	           
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $( "#house-detail" ).dialog({
                resizable: false,
                
                width:980,
                modal: true,
                buttons: {
                    "Add": function() {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/house/addremarks/"+id,
                            data: ({remarks : $("#house-remark").val()}),
                            success: function(msg){
                                $("#house-detail").load("<?= $this->baseURI(); ?>planning/house/housedetail/"+id);
                            }
                        }); 
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
            $("#house-detail").load("<?= $this->baseURI(); ?>planning/house/housedetail/"+id);
        });      
        
    }
    function deleteHouse(id){
                
        $(function() {	
           
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $( "#delete-confirm" ).dialog({
                resizable: false,
                height:200,
                modal: true,
                buttons: {
                    "Delete": function() {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/house/delete/"+id,                            
                            success: function(msg){
                                $("#house-listing").load("<?= $this->baseURI(); ?>planning/house/list");
                            }
                        }); 
                        $( this ).dialog( "close" );
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
            
        });
    }
    
    $(function() {		
        $( "#dialog:ui-dialog" ).dialog( "destroy" );        

        function updateTips( t ) {
            tips = $( ".validateTips" );
            tips
            .text( t )
            .addClass( "ui-state-highlight" );
            setTimeout(function() {
                tips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
        }

        function checkLength( o, n, min, max ) {
            
            if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                    min + " and " + max + "." );
                return false;
            } else {
                return true;
            }
        }     
        
        function checkEmpty( o, n, min, max ) {
            if ( o.val().length < 1 ) {
                o.addClass( "ui-state-error" );
                updateTips( n + " must not be between empty");
                return false;
            } else {
                return true;
            }
        }
       
        $( "#add-house-form" ).dialog({
            autoOpen: false,
             width: 750,
            modal: true,
            buttons: {
                "Add": function() {
                    var bValid = true;
                    var ownerName = $( "#ownerName" ),
                    address = $("#address"),
                    officialAddress = $("#officialAddress"),
                    postnr = $("#postnr"),
                    postadress = $("#postadress"),
                    customerCode = $("#customerCode"),
                    regDate = $('#regDate'),
                    seller = $('#seller'),                    
                    personalNo = $('#personalNo'),
                    tel1 = $('#tel1'),
                    tel2 = $('#tel2'),
                    email = $('#email'),
                    avropad = $('#avropad'),
                    blastKlar = $('#blastKlar'),
                    aviseradVecka = $('#aviseradVecka'),
                    installKlar = $('#installKlar'),
                    installeradAv = $('#installeradAv'),                    
                    cabinet = $("#cabinetList"),
                    switchList = $("#switchList"),
                    status = $( "#status" ),

                    
                    allFields = $( [] ).add( ownerName ).add( customerCode ).add(address).add( officialAddress ).add( status ).add(cabinet).add(switchList),
                    tips = $( ".validateTips" );
                    
                    allFields.removeClass( "ui-state-error" );

                    bValid = bValid && checkLength( ownerName, "owner name", 2, 16 );
                    bValid = bValid && checkLength( address, "address", 2, 16 );
                    bValid = bValid && checkLength( officialAddress, "official address", 2, 16 );
                    bValid = bValid && checkLength( customerCode, "customer code", 2, 16 );
                    bValid = bValid && checkEmpty( cabinet, "cabinet name", 2, 16 );
                    bValid = bValid && checkEmpty( switchList, "switch name", 2, 16 );
                                        
                    if ( bValid ) {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/house/add",
                            data: ({ownerName : ownerName.val(),address:address.val(),officialAddress:officialAddress.val(),customerCode:customerCode.val(),cabinetList:cabinet.val(),switchList:switchList.val(),status : status.val(),
                                regDate:regDate.val(),seller:seller.val(),personalNo:personalNo.val(),tel1:tel1.val(),tel2:tel2.val(),email:email.val(),avropad:avropad.val(),blastKlar:blastKlar.val(),aviseradVecka:aviseradVecka.val(),
                                installKlar:installKlar.val(),installeradAv:installeradAv.val(),postnr:postnr.val(),postadress:postadress.val()
                            }),
                            success: function(msg){
                                $("#house-listing").load("<?= $this->baseURI(); ?>planning/house/list");
                            }
                        });                       
                        $( this ).dialog( "close" );
                        $('#add-house-form').html('');
                    }
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                    $('#add-house-form').html('');
                }
            },
            close: function() {
                $('#add-house-form').html('');
                //   allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });
        
        $( "#edit-house-form" ).dialog({
            autoOpen: false,
             width: 750,
            modal: true,
            buttons: {
                "Edit": function() {
                    var bValid = true;
                    var ownerName = $( "#ownerName" ),
                    address = $("#address"),
                    officialAddress = $("#officialAddress"),
                    postnr = $("#postnr"),
                    postadress = $("#postadress"),
                    customerCode = $("#customerCode"),
                    regDate = $('#regDate'),
                    seller = $('#seller'),
                    personalNo = $('#personalNo'),
                    tel1 = $('#tel1'),
                    tel2 = $('#tel2'),
                    email = $('#email'),
                    avropad = $('#avropad'),
                    blastKlar = $('#blastKlar'),
                    aviseradVecka = $('#aviseradVecka'),
                    installKlar = $('#installKlar'),
                    installeradAv = $('#installeradAv'),
                    cabinet = $("#cabinetList"),
                    switchList = $("#switchList"),
                    status = $( "#status" ),
                    
                    allFields = $( [] ).add( ownerName ).add( customerCode ).add(address).add( officialAddress ).add( status ).add(cabinet).add(switchList),
                    tips = $( ".validateTips" );
                    
                    allFields.removeClass( "ui-state-error" );

                    bValid = bValid && checkLength( ownerName, "owner name", 2, 16 );
                    bValid = bValid && checkLength( address, "address", 2, 16 );
                    bValid = bValid && checkLength( officialAddress, "official address", 2, 16 );
                    bValid = bValid && checkLength( customerCode, "customer code", 2, 16 );
                    bValid = bValid && checkEmpty( cabinet, "cabinet name", 2, 16 );
                    bValid = bValid && checkEmpty( switchList, "switch name", 2, 16 );
                    
                    if ( bValid ) {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/house/edit/"+houseId,
                            data: ({ownerName : ownerName.val(),address:address.val(),officialAddress:officialAddress.val(),customerCode:customerCode.val(),cabinetList:cabinet.val(),switchList:switchList.val(),status : status.val(),
                            regDate:regDate.val(),seller:seller.val(),personalNo:personalNo.val(),tel1:tel1.val(),tel2:tel2.val(),email:email.val(),avropad:avropad.val(),blastKlar:blastKlar.val(),aviseradVecka:aviseradVecka.val(),
                                installKlar:installKlar.val(),installeradAv:installeradAv.val(),postnr:postnr.val(),postadress:postadress.val()
                            }),
                            success: function(msg){
                                $("#house-listing").load("<?= $this->baseURI(); ?>planning/house/list");
                            }
                        });                       
                        $( this ).dialog( "close" );
                        $('#edit-house-form').html('');
                    }
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                    $('#edit-house-form').html('');
                }
            },
            close: function() {
                $('#edit-house-form').html('');
                //   allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });

    });

</script>
<div class="dataBoxOver">
    <div class="dataInnRep">
        <div class="dataInnTop" id="house-listing">
            <?php echo $this->render('house/list.phtml'); ?>		
        </div>	
    </div>
</div>
<div id="add-house-form" title="Add House"></div>
<div id="edit-house-form" title="Edit House"></div>
<div id="house-detail"></div>
<div id="delete-confirm" title="Delete House" style="display: none;">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This house will be permanently deleted. Are you sure?</p>
</div>